/*
 * Copyright (C) 2001-2005 Pleasant nightmare studio
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 *
 * See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */
package org.pleasantnightmare.gblade.data;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * @author deus
 * @version 1.0
 * @since Sep 9, 2009 12:07:49 PM
 */
public class ComponentData {
  private ComponentType componentType;
  private String componentName;
  private ComponentData parent;
  private List<ComponentData> children;
  private List<PropertyData> properties;

  public ComponentData() {
    this(ComponentType.UNDEFINED, "UNDEFINED");
  }

  public ComponentData(ComponentType componentType, String componentName) {
    this.componentType = componentType;
    this.componentName = componentName;
    children = new ArrayList<ComponentData>();
    properties = new ArrayList<PropertyData>();
  }

  public void addComponentData(ComponentData child) {
    children.add(child);
  }

  public void removeComponentData(ComponentData child) {
    children.remove(child);
  }

  public boolean containsComponentData(ComponentData child) {
    return children.contains(child);
  }

  public List<ComponentData> getChildren() {
    return children;
  }

  public void addPropertyData(PropertyData property) {
    properties.add(property);
  }

  public void addPropertyData(String name, Object value) {
    properties.add(new PropertyData(name, value));
  }

  public void removePropertyData(PropertyData property) {
    properties.remove(property);
  }

  public void removePropertyData(String name) {
    Iterator<PropertyData> it = properties.iterator();
    while (it.hasNext()) {
      PropertyData property = it.next();
      if (property.getName().equals(name)) {
        it.remove();
        return;
      }
    }
  }

  public boolean containsPropertyData(PropertyData property) {
    return properties.contains(property);
  }

  public List<PropertyData> getProperties() {
    return properties;
  }

  public PropertyData getPropertyByName(String propertyName) {
    if (propertyName == null)
      throw new IllegalArgumentException("propertyName = null");

    for (PropertyData property : properties) {
      if (propertyName.equals(property.getName()))
        return property;
    }

    throw new IllegalArgumentException("Unexistent property queried: " + propertyName);
  }

  public Object getPropertyValueByName(String propertyName) {
    return getPropertyByName(propertyName).getValue();
  }

  public ComponentType getComponentType() {
    return componentType;
  }

  public void setComponentType(ComponentType componentType) {
    this.componentType = componentType;
  }

  public String getComponentName() {
    return componentName;
  }

  public void setComponentName(String componentName) {
    this.componentName = componentName;
  }

  public ComponentData getParent() {
    return parent;
  }

  public void setParent(ComponentData parent) {
    this.parent = parent;
  }

  @Override public String toString() {
    return String.format("ComponentData{componentType=%s, componentName='%s'\'', parent=%s'}'", componentType, componentName, parent);
  }
}
